package com.salton123.soulove.biz_main.main

import android.Manifest
import android.content.pm.PackageManager
import android.os.Bundle
import androidx.core.content.ContextCompat
import com.alibaba.android.arouter.facade.annotation.Route
import com.qw.soul.permission.SoulPermission
import com.qw.soul.permission.bean.Permission
import com.qw.soul.permission.bean.Permissions
import com.qw.soul.permission.callbcak.CheckRequestPermissionsListener
import com.salton123.soulove.app.R
import com.salton123.soulove.common.Constants
import com.salton123.soulove.sailfish.video.task.BackgroundTaskManager
import com.salton123.soulove.sailfish.video.task.DocumentFileScanTask
import com.salton123.soulove.sailfish.video.task.MediaFileScanTask
import com.salton123.soulove.sailfish.video.ui.fm.VideoCategoryFragment
import com.salton123.soulove.utils.Utils
import com.salton123.ui.base.BaseActivity
import io.github.dreierf.materialintroscreen.SlideFragment
import java.util.concurrent.atomic.AtomicBoolean

/**
 * Time:2021/9/23 9:24 下午
 * Author:
 * Description:
 */
@Route(path = Constants.Router.App.MAIN)
class VideoMainActivity : BaseActivity() {
    override fun enableTitleBar(): Boolean = false
    override fun getLayoutId(): Int = R.layout.activity_video_main
    override fun initViewAndData(savedInstanceState: Bundle?) {
        loadRootFragment(R.id.fl_container, VideoCategoryFragment())
    }

    private val hasLaunchedTask: AtomicBoolean = AtomicBoolean(false)

    private fun launchTask() {
        if (!hasLaunchedTask.get()) {
            if (hasStoragePermission()) {
                BackgroundTaskManager.apply {
                    register(MediaFileScanTask())
                    if (Utils.isAboveAndroid11()) {
                        register(DocumentFileScanTask())
                    }
                    launch()
                    hasLaunchedTask.set(true)
                }
            }
        }
    }

    override fun onResume() {
        super.onResume()
        launchTask()
    }

    val permissions = arrayOf(
        Manifest.permission.WRITE_EXTERNAL_STORAGE,
        Manifest.permission.READ_EXTERNAL_STORAGE
    )

    private val isPermissionDialogShow: AtomicBoolean = AtomicBoolean(false)
    private fun hasStoragePermission(): Boolean {
        if (!hasPermissionsToGrant(*permissions)) {
            if (!isPermissionDialogShow.get()) {
                isPermissionDialogShow.set(true)
                SoulPermission.getInstance().checkAndRequestPermissions(
                    Permissions.build(*permissions), object : CheckRequestPermissionsListener {
                        override fun onAllPermissionOk(allPermissions: Array<Permission>) {
                            launchTask()
                            isPermissionDialogShow.set(false)
                        }

                        override fun onPermissionDenied(refusedPermissions: Array<Permission>) {
                            isPermissionDialogShow.set(false)
                        }
                    })
                return false
            }
        }
        return true
    }

    private fun hasPermissionsToGrant(vararg permissions: String): Boolean {
        for (permission in permissions) {
            if (SlideFragment.isNotNullOrEmpty(permission)) {
                if (ContextCompat.checkSelfPermission(activity(), permission) != PackageManager.PERMISSION_GRANTED
                ) {
                    return true
                }
            }
        }
        return false
    }

    override fun onDestroy() {
        super.onDestroy()
        BackgroundTaskManager.unregisterAll()
    }
}